﻿using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.IRepository.Sys;
using net_work.IService.Sys;

namespace net_work.Service.Sys
{
    public class UserService(IUserRepository userRepository, IPersonRepository personRepository) : IUserService
    {
        public Task<PagedResult<User>> GetUsersPagedAsync(PagedRequest request)
        {
            return userRepository.GetPagedAsync(request);
        }

        public Task<User?> SelectByIdAsync(long userId)
        {
            return userRepository.GetByIdAsync(userId);
        }

        public async Task<long> InsertUser(UserAddRequest userAddRequest)
        {
            if (await personRepository.GetByIdAsync(userAddRequest.PsnId) == null)
            {
                return -1;
            }

            return await userRepository.InsertAsync(userAddRequest);
        }

        public async Task<int> UpdateUser(UserUpdateRequest userUpdateRequest)
        {
            if (userUpdateRequest.Id <= 0)
            {
                return -1;
            }

            if (await personRepository.GetByIdAsync(userUpdateRequest.PsnId) == null)
            {
                return -1;
            }

            return await userRepository.UpdateAsync(userUpdateRequest) ? 1 : -1;
        }

        public async Task<int> DeleteUserByIds(List<long> ids)
        {
            return await userRepository.DeleteByIdsAsync(ids) ? 1 : -1;
        }
    }
}